<template>
  <div class="bigbox">
    <!-- 表头盒子 -->
    <!-- 盒子主体 -->
    <div class="mainbox">
      <div class="top">
        <div class="left">
          <div class="search">
            <el-input
              placeholder="请输入关键字搜索"
              prefix-icon="el-icon-search"
              v-model="inputValue"
            >
            </el-input>
          </div>
        </div>
        <div class="towBtn">
          <button @click="chongzhi" class="button">重置</button>
          <button @click="chaxun" class="button chaxun">查询</button>
        </div>
      </div>
      <div class="bodybox">
        <!-- 新增按钮 -->
        <!-- 循环图片盒子 -->
        <div class="table">
          <div class="titlebox">
            <div class="text"></div>
            <div
              class="right"
              :style="{ alignItems: 'center', lineHeight: '32.5px' }"
            >
              <button
                type="primary"
                size="small"
                class="bewdao daochu"
                @click="sync"
              >
                同步
              </button>
              <button
                type="primary"
                size="small"
                class="bewdao daochu"
                @click="addOpen"
              >
                新增
              </button>
              <button
                type="primary"
                size="small"
                class="bewdao daochu"
                @click="indexOpen"
              >
                索引管理
              </button>
              <el-tooltip
                class="item"
                effect="dark"
                content="刷新"
                placement="top"
              >
                <span
                  @click="shuaxin"
                  :style="{ cursor: 'pointer', fontSize: '20px' }"
                  ><i class="el-icon-refresh-right"></i
                ></span>
              </el-tooltip>
              <el-dropdown trigger="click" @command="command">
                <span class="el-dropdown-link">
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="密度"
                    placement="top"
                  >
                    <span :style="{ cursor: 'pointer', fontSize: '20px' }"
                      ><i class="el-icon-d-caret"></i
                    ></span>
                  </el-tooltip>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item class="dropdown-list" command="默认"
                    >默认</el-dropdown-item
                  >
                  <el-dropdown-item class="dropdown-list" command="中等"
                    >中等</el-dropdown-item
                  >
                  <el-dropdown-item class="dropdown-list" command="紧凑"
                    >紧凑</el-dropdown-item
                  >
                </el-dropdown-menu>
              </el-dropdown>
            </div>
          </div>
          <el-table
            v-loading="loading"
            :data="tableList"
            style="width: 99%"
            border
            height="400px"
            id="exportTabyong"
            ref="table"
            :size="iissize"
            :header-cell-style="{
              background: '#f5f5f5',
              'text-align': 'center',
            }"
          >
            <template slot="empty">
              <el-empty description="暂无数据"></el-empty>
            </template>
            <el-table-column align="left" prop="bizFieldCode" label="属性编码">
            </el-table-column>
            <el-table-column
              align="left"
              prop="tableColumnName"
              label="表字段名"
            >
            </el-table-column>
            <el-table-column align="left" prop="bizFieldName" label="属性名称">
            </el-table-column>
            <el-table-column
              align="left"
              width="80px"
              prop="nullEnable"
              label="是否可空"
            >
              <template slot-scope="scope">
                <span>{{ scope.row.nullEnable ? "是" : "否" }}</span>
              </template>
            </el-table-column>
            <el-table-column align="left" prop="dataType" label="数据类型">
            </el-table-column>
            <el-table-column
              align="right"
              width="80px"
              prop="dataLength"
              label="数据长度"
            >
            </el-table-column>
            <el-table-column
              align="right"
              width="80px"
              prop="dataPrecision"
              label="数据精度"
            >
            </el-table-column>
            <el-table-column align="center" label="操作">
              <template slot-scope="scope">
                <div class="table-cz">
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="编辑"
                    placement="top"
                  >
                    <i
                      style="
                        font-size: 14px;
                        color: rgb(64, 149, 229);
                        cursor: pointer;
                      "
                      class="el-icon-edit"
                      @click="edit(scope.row)"
                    ></i
                  ></el-tooltip>
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="删除"
                    placement="top"
                  >
                    <i
                      style="font-size: 14px; color: red; cursor: pointer"
                      class="el-icon-delete"
                      @click="del(scope.row)"
                    ></i
                  ></el-tooltip>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <!-- 新增模型属性 -->
        <el-dialog
          :show-close="true"
          :visible.sync="addVisible"
          v-if="addVisible"
          width="500px"
          :close-on-click-modal="false"
          :destroy-on-close="true"
          :before-close="diaClose"
          append-to-body
          :center="true"
        >
          <div class="dialog">
            <el-form
              ref="dialogForm"
              :rules="rules"
              :model="dialogForm"
              label-width="110px"
            >
              <el-form-item label="属性编码" prop="bizFieldCode">
                <el-input v-model="dialogForm.bizFieldCode"></el-input>
              </el-form-item>
              <el-form-item label="表字段名" prop="tableColumnName">
                <el-input v-model="dialogForm.tableColumnName"></el-input>
              </el-form-item>
              <el-form-item label="属性名称" prop="bizFieldName">
                <el-input v-model="dialogForm.bizFieldName"></el-input>
              </el-form-item>
              <el-form-item label="属性类型" prop="dataType">
                <el-input v-model="dialogForm.dataType"></el-input>
              </el-form-item>
              <div
                style="
                  display: flex;
                  align-items: center;
                  justify-content: space-between;
                "
              >
                <el-form-item
                  style="width: 50%"
                  label="数据长度"
                  prop="dataLength"
                >
                  <el-input v-model="dialogForm.dataLength"></el-input>
                </el-form-item>
                <el-form-item
                  style="width: 50%"
                  label="数据精度"
                  prop="dataPrecision"
                >
                  <el-input v-model="dialogForm.dataPrecision"></el-input>
                </el-form-item>
              </div>
              <div
                style="
                  display: flex;
                  align-items: center;
                  justify-content: space-between;
                "
              >
                <el-form-item
                  style="width: 50%"
                  label="是否可空"
                  prop="nullEnable"
                >
                  <el-switch
                    style="display: block"
                    v-model="dialogForm.nullEnable"
                    active-color="#1e8e3e"
                    inactive-color="#868686"
                  >
                  </el-switch>
                </el-form-item>
                <el-form-item
                  style="width: 50%"
                  label="是否只读"
                  prop="readOnly"
                >
                  <el-switch
                    style="display: block"
                    v-model="dialogForm.readOnly"
                    active-color="#1e8e3e"
                    inactive-color="#868686"
                  >
                  </el-switch>
                </el-form-item>
              </div>
              <el-form-item label="自动设值策略" prop="autoValueStrategy">
                <el-select
                  v-model="dialogForm.autoValueStrategy"
                  placeholder=""
                >
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="属性默认值" prop="defaultValue">
                <el-input v-model="dialogForm.defaultValue"></el-input>
              </el-form-item>
            </el-form>
          </div>
          <div style="margin-top: 10px; text-align: center">
            <span slot="footer" class="dialog-footer">
              <el-button @click="diaClose">取 消</el-button>
              <el-button type="primary" @click="add">保 存</el-button>
            </span>
          </div>
        </el-dialog>
        <el-dialog
          :show-close="true"
          :visible.sync="indexVisible"
          v-if="indexVisible"
          width="1000px"
          :close-on-click-modal="false"
          :destroy-on-close="true"
          :before-close="diaClose"
          append-to-body
          :center="true"
        >
          <IndexMaintain :data="data"></IndexMaintain>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import IndexMaintain from './IndexMaintain.vue'
import {
  addModelPropertyApi,
  delModelPropertyApi,
  editModelPropertyApi,
  getPropertyListApi,
  getPropertyModelApi,
  syncModelPropertyApi
} from '@/api/Model-all/Model-business'
export default {
  name: 'TableMaintain',
  components: {
    IndexMaintain
  },
  props: {
    data: {
      type: Object,
      default: () => {}
    }
  },
  data () {
    return {
      inputValue: '',
      isEdit: 'add',
      options: [
        { label: '无', value: 'none' },
        { label: 'UUID', value: 'uuid' },
        { label: '自增长', value: 'increment' }
      ],
      loading: false,
      loading2: false,
      iissize: 'medium',
      dialogForm: {
        nullEnable: true,
        readOnly: false,
        autoValueStrategy: 'none'
      },
      rules: {
        bizFieldCode: [
          { required: true, message: '请输入属性编码', trigger: 'blur' }
        ],
        tableColumnName: [
          { required: true, message: '请输入表字段名', trigger: 'blur' }
        ],
        bizFieldName: [
          { required: true, message: '请输入属性名称', trigger: 'blur' }
        ],
        dataType: [
          { required: true, message: '请输入属性类型', trigger: 'blur' }
        ],
        dataLength: [
          { required: true, message: '请输入数据长度', trigger: 'blur' }
        ],
        // dataPrecision: [
        //   { required: true, message: '请输入数据精度', trigger: 'blur' }
        // ]
        nullEnable: [
          { required: true, message: '请选择是否可空', trigger: 'change' }
        ],
        readOnly: [
          { required: true, message: '请选择是否只读', trigger: 'change' }
        ]
      },
      deflist: [],
      tableList: [],
      addVisible: false,
      indexVisible: false
    }
  },
  mounted () {
    this.getTableList()
  },
  methods: {
    // 查询业务模型属性列表
    async getTableList () {
      this.loading = true
      const res = await getPropertyListApi(this.data.bizModelId)
      this.deflist = res.data.list
      this.tableList = res.data.list
      this.loading = false
    },
    // 新增窗口
    async addOpen () {
      this.isEdit = 'add'
      this.addVisible = true
    },
    // 索引弹窗打开
    indexOpen () {
      this.indexVisible = true
    },
    // 弹窗关闭
    diaClose () {
      this.addVisible = false
      this.indexVisible = false
      this.dialogForm = {
        nullEnable: true,
        readOnly: false,
        autoValueStrategy: 'none'
      }
    },
    async shuaxin () {
      await this.chaxun()
      this.$message({
        message: '列表刷新成功',
        type: 'success',
        duration: 2000
      })
    },
    async chaxun () {
      await this.getTableList()
      const list = [...this.deflist]
      if (this.inputValue) {
        this.tableList = list.filter(
          (item) =>
            item.bizFieldCode.includes(this.inputValue) ||
            item.tableColumnName.includes(this.inputValue)
        )
      } else {
        this.tableList = [...list]
      }
    },
    command (val) {
      if (val === '默认') {
        this.iissize = 'medium'
      } else if (val === '中等') {
        this.iissize = 'small'
      } else {
        this.iissize = 'mini'
      }
    },
    // 新增/修改提交
    async add () {
      await this.$refs.dialogForm.validate()
      console.log(this.dialogForm)

      const res =
        this.isEdit === 'add'
          ? await addModelPropertyApi({
            bizModelId: this.data.bizModelId,
            ...this.dialogForm
          })
          : await editModelPropertyApi(this.dialogForm)
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: 'success',
          duration: 2000
        })
        this.getTableList()
        this.diaClose()
      } else {
        this.$message({
          type: 'error',
          message: res.message,
          duration: 2000
        })
      }
    },
    // 同步
    async sync () {
      this.$confirm('是否确认从数据库同步模型信息？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await syncModelPropertyApi({
            bizModelId: this.data.bizModelId
          })
          console.log(res)
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: res.message,
              duration: 2000
            })
          } else {
            this.$message({
              type: 'error',
              message: res.message,
              duration: 2000
            })
          }
          this.getTableList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消',
            duration: 2000
          })
        })
    },
    // 编辑
    async edit (item) {
      console.log(item)
      this.isEdit = 'edit'
      const res = await getPropertyModelApi(item.bizFieldId)
      console.log(res)
      const data = res.data.entity
      this.dialogForm = data
      // this.dialogForm = {
      //   fieldName: data.fieldName,
      //   fieldLength: data.fieldLength,
      //   fieldAccuracy: data.fieldAccuracy,
      //   nullEnable: data.nullEnable,
      //   defaultValue: data.defaultValue,
      //   fieldExplain: data.fieldExplain,
      //   fieldCode: data.fieldCode,
      //   fieldId: data.fieldId,
      //   fieldType: data.fieldType
      // }
      // this.$forceUpdate()
      this.addVisible = true
    },
    // 删除
    async del (item) {
      this.$confirm('是否确认删除属性字段？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          try {
            const res = await delModelPropertyApi(item.bizFieldId)
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.message,
                duration: 2000
              })
              this.getTableList()
            } else {
              this.$message({
                type: 'error',
                message: res.message,
                duration: 2000
              })
            }
          } catch (e) {
            console.log(e)
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    chongzhi () {
      this.inputValue = ''
      this.getTableList()
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep textarea {
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
}
:v-deep.el-table th.gutter{
  display: none;
  width:0
}
::v-deep.el-table colgroup col[name='gutter']{
  display: none;
  width: 0;
}
::v-deep.el-table__body{
  width: 100% !important;
}
.table-cz {
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.dialog-footer {
  display: flex;
  justify-content: center;
}
.bigbox {
  background-color: #f1f1f1;

  padding: 9px;
  display: flex;
  .mainbox {
    position: relative;
    flex: 1;
    .title {
      position: absolute;
      display: flex;
      font-size: 20px;
      top: -48px;
      left: 0;
      .code {
        width: 400px;
        margin-right: 50px;
      }
      .name {
        margin-right: 50px;
      }
      .edit {
        color: #4095e5;
        cursor: pointer;
      }
    }
    .top {
      padding: 0 20px;
      display: flex;
      justify-content: space-between;
      width: 100%;
      height: 80px;
      background-color: #fff;
      border-bottom: 1px solid #f1f1f1;
      margin-bottom: 10px;
      align-items: center;
      .left {
        display: flex;
        align-items: center;
        .group-select {
          width: 200px;
          margin-right: 10px;
        }
        .group-add {
          display: flex;
          justify-content: center;
          align-items: center;
          cursor: pointer;
          border: 1px solid #dcdfe6;
          border-radius: 5px;
          width: 25px;
          height: 25px;
          margin-right: 20px;
        }
        .search {
          display: flex;
          align-items: center;
          width: 200px;
        }
      }
    }
    .bodybox {
      position: relative;
      background-color: #fff;
      padding: 20px;
      padding-bottom: 30px;
      .pagination {
        position: absolute;
        right: 20px;
        bottom: 10px;
      }
      .table {
        .titlebox {
          display: flex;
          justify-content: space-between;
          margin-bottom: 10px;
          .text {
            font-size: 16px;
          }
          .right {
            align-items: center;
            i {
              margin-left: 10px;
            }
            .bewdao {
              width: 70px;
              height: 32px;
              margin-right: 20px;
            }
            .daochu {
              background-color: #1890ff;
              color: #fff;
              border: none;
              border-radius: 3px;
            }
          }
        }
      }
      .dialog {
        .title {
          font-size: 28px;
          margin-bottom: 30px;
        }
        .text {
          font-size: 12px;
          color: #3b8eea;
        }
      }
    }
  }
}

::v-deep .el-select {
  width: 100%;
}

::v-deep .el-switch {
  height: 40px;
  line-height: 35px;
}

::v-deep .el-input {
  height: 32px;
}
::v-deep .el-input__inner {
  height: 32px;
}
::v-deep .el-input__icon {
  line-height: 1;
}
.el-select-dropdown__item {
  padding-left: 10px;
}
::v-deep .el-date-editor {
  width: 100%;
}

.el-form-item {
  margin-bottom: 20px;
}
.button {
  width: 63px;
  height: 32px;
  margin-right: 5px;
  background-color: #fff;
  border: 1px solid #ccc;
  border-radius: 3px;
}
.chaxun {
  background-color: #1890ff;
  color: #fff;
}
::v-deep .el-range-separator {
  line-height: 25px;
}
.timeTitle {
  height: 50px;
}

.dropdown-list {
  padding: 0;
  text-align: center;
  width: 50px;
}

.change-type {
  width: 100px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  border-radius: 4px;
  background-color: rgba(255, 255, 255, 1);
  font-size: 14px;
  color: #606266;
  margin-right: 10px;
}

.node-img {
  width: 16px;
  height: 16px;
  margin-right: 5px;
  vertical-align: bottom;
}
</style>
